﻿using Castle.DynamicProxy.Generators;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MiniErp.Common.Cache;
using MiniErp.Entity.JWTs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

/// <summary>
/// 缓存管理系统
/// </summary>
namespace MiniErp.Core.Controllers.CacheManagement
{
    /// <summary>
    /// 缓存管理控制器
    /// </summary>
    [Route("Cache")]
    public class CacheController : ApiControllers
    {
        private readonly ICache _cache;

        public CacheController(ICache cache)
        {
            _cache = cache;
        }

        /// <summary>
        /// 获取权限缓存列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetRightCache")]
        public async Task<Dictionary<string, List<CacheUserRights>>> GetRightCache()
        {
            var keys = await _cache.GetAsyncByWildCard($"{CacheKeyType.Right}*");
            if (keys == null) return null;
            var keyvaluedic = new Dictionary<string, List<CacheUserRights>>();
            foreach (var key in keys)
            {
                var value = await _cache.GetAsync<List<CacheUserRights>>(CacheKeyType.Right, key.Replace(CacheKeyType.Right, ""));
                keyvaluedic.Add(key, value);
            }
            return keyvaluedic;
        }

        /// <summary>
        /// 清除权限缓存
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <returns></returns>
        [HttpDelete]
        [Route("DeleteRightCache")]
        public Task<long> DeleteRightCache(string cacheKey)
        {
            return _cache.DelAsync(CacheKeyType.Right, cacheKey);
        }
    }
}
